import {ColorStr, LogType} from "../../../type/Type";
import {ConfigKey} from "../../../constant/ConfigConstant";
import Configer from "./Configer";
import {PlayerModel} from "../adapterModel/model4/PlayerModel";
import {FinalKey} from "../../../constant/FinalConstant";
import {LocalSaveModel} from "../adapterModel/model1/LocalSaveModel";
import {GlobalModel} from "../adapterModel/model1/GlobalModel";
import {CodeUtil} from "../../codeAdapter/CodeUtil";

export class Logger {

    constructor() {
        const logPath = Configer.getFinal(FinalKey.logPath);
        LocalSaveModel.setPath(logPath);
    }

    static toastError(text: string, player?: PlayerModel) {
        let str = CodeUtil.getColorStr(ColorStr.purple, text)
        this.toast(str, LogType.error, player)
    }
    static toastSys(text: string, player?: PlayerModel) {
        this.toast(text, LogType.SysDev)
    }
    static toastAttr(text: string, player?: PlayerModel) {
        this.toast(text, LogType.attrDev, player)
    }
    static toastSkill(text: string, player?: PlayerModel) {
        this.toast(text, LogType.skillDev, player)
    }
    static toastTask(text: string, player?: PlayerModel) {
        this.toast(text, LogType.taskDev, player)
    }
    static toastSave(text: string, player?: PlayerModel) {
        this.toast(text, LogType.saveDev, player)
    }
    static toastMix(text: string, player?: PlayerModel) {
        this.toast(text, LogType.mixDev, player)
    }
    static toastWarn(text: string, player?: PlayerModel) {
        this.toast(text, LogType.warn)
    }
    static toastProduct(text: string, player?: PlayerModel, color?: ColorStr) {
        let str = text;
        if (color) {
            str = CodeUtil.getColorStr(color, text)
        }
        this.toast(str, LogType.product, player)
    }
    static toast (text: string, logTypes: LogType = LogType.error, player?: PlayerModel) {
        const saveFun = () => {
            let newText = text;
            LocalSaveModel.saveLog(newText);
        }
        const fun = () => {
            let newText = text;
            if (player?.isValid()) {
                const playerId = player.playerId;
                GlobalModel.toast(newText, playerId);
            } else {
                GlobalModel.toast(newText);
            }
        }
        if (text) {
            switch (logTypes) {
                case LogType.error:{
                    fun();
                    break;
                }
                case LogType.product:{
                    fun();
                    break;
                }
                case LogType.warn:{
                    if (Configer.getConfig(ConfigKey.mode) === LogType.product) {
                    } else {
                        fun();
                    }
                    break;
                }
                case Configer.getConfig(ConfigKey.mode): {
                    fun();
                    break;
                }
            }

            const saveMode = Configer.getConfig(ConfigKey.saveMode);
            if (saveMode && saveMode >= 0) {
                switch (logTypes) {
                    case saveMode: {
                        saveFun();
                        break;
                    }
                }
            }
        }
    }
}